import {
	userLoginApi,
	decyptPhoneApi,
	getClientInfoApi,
	getUserInfoApi
} from '@/api/user.js'

export default {
	namespaced: true,
	state: {
		userInfo: '', //用户信息
	},
	mutations: {
		SET_USERINFO(state, userOL) {
			state.userInfo = userOL;
		}
	},
	actions: {
		//湖区并保存token
		getToken() {
			return new Promise(resolve => {
				uni.login({
					success(res) {
						resolve(res.code)
					}
				})
			}).then(code => {
				return customerAuthApi({
					code
				})
			}).then(data => {
				uni.setStorageSync('token', data.data.access_token);
				return Promise.resolve()
			})
		},
		//获取地址授权
		authAddress() {
			return new Promise((resolve, reject) => {
				uni.getFuzzyLocation({
					success(res) {
						resolve(res)
					},
					fail(error) {
						if (error.errMsg === 'getFuzzyLocation:fail auth deny') {
							uni.showModal({
								title: '温馨提示',
								content: `请允许小程序使用您的'位置信息'，方便我们给您提供更好的服务`,
								showCancel: false,
								success() {
									uni.openSetting()
								}
							})
						}
					}
				})
			}).then(({latitude,longitude}) => {
				return new Promise(resolve => {
					vue.$qqmapsdk.reverseGeocoder({
						location: {
							latitude,
							longitude
						},
						sig: '9FGBtABBbGqHaEXLFMRdq3crIsixO5Z6',
						success(res) {
							resolve(res.result.address)
						}
					})
				})
			})
		},
		//授权scope.camera，scope.record
		authLivePusher() {
			return new Promise(resolve => {
				wx.authorize({
					scope: 'scope.camera',
					success() {
						// 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
						resolve()
					},
					fail(error) {
						uni.showModal({
							title: '重要提示！',
							title: '拒绝摄像头授权，将不能进行视频通话，请在设置开启摄像头权限',
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.openSetting()
								}
							}
						})
					}
				})
			}).then(data => {
				return new Promise(resolve => {
					uni.authorize({
						scope: 'scope.record',
						success() {
							resolve()
						},
						fail() {
							uni.showModal({
								title: '重要提示！',
								content: '拒绝麦克风授权，将不能进行视频通话，请在设置开启麦克风权限',
								showCancel: false,
								success(res) {
									if (res.confirm) {
										uni.openSetting()
									}
								}
							})
						}
					})
				})
			})
		},
		/* 获取用户手机号 */
		setPhoneByUserInfo(context, {
			encryptedData,
			iv
		}) { 
			return decyptPhoneApi({
				keyStr: context.state.userInfo.sessionKey,
				encDataStr: encryptedData,
				ivStr: iv,
			}).then(data => {
				return getClientInfoApi({
					phone:data.phoneNumber,
					userName:context.state.userInfo.nickName
				})
			}).then(data => {
				// 将token存在本地
				uni.setStorageSync('token', data.data.token);
				
				Promise.resolve();
			})
		},
		/* 微信登录 */
		login(context) {
			let edDataList = '';
			
			new Promise(resolve => {
				uni.getUserProfile({
					lang: 'zh_CN',
					desc: '用户登录',
					success(res) {
						resolve(res);
					},
					fail() {
						uni.showToast({
							title: '您取消了授权，请重新授权',
							icon:'none'
						})
					}
				})
			}).then(edData => {
				edDataList = edData;
				return new Promise(resolve => {
					uni.login({
						success(res) {
							resolve(res.code)
						}
					})
				})
			}).then(code => {
				//获取openid和sessionKey
				return userLoginApi(code)
			}).then((data) => {
				// 设置手机号
				context.commit('SET_USERINFO',{
					...edDataList.userInfo,
					sessionKey: data.session_key,
					openid: data.openid
				})
				
				Promise.resolve();
			})
		}
	}
}
